package com.ygsj;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ygsj.Utils.OkhttpUtils;
import com.ygsj.Utils.ParJson;
import com.ygsj.Utils.UHFUtils;
import com.ygsj.ui.loader.Loader;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Response;

public class SignUpActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "SignUpActivity";
    EditText et_station;
    String userName,pwd,imei,staionName,dbname,name_person, mmname ,uuname;
    private UHFUtils uhfUtils;
    boolean networkAvailable;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    //Loader.stopLoading();
                    Map<String,String> map_response = (Map<String, String>) msg.obj;
                    String flag = map_response.get("flag");
                    String msg_ = map_response.get("msg");
                    if (!TextUtils.isEmpty(flag)){
                        if (flag.equals("0")|| flag.equals("2")){
                            Toast.makeText(SignUpActivity.this, msg_, Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(SignUpActivity.this,MainActivity.class);
                            intent.putExtra("uuname",uuname);
                            intent.putExtra("mmname",map_response.get("mmname"));
                            intent.putExtra("userName",userName);
                            intent.putExtra("pwd",pwd);
                            intent.putExtra("imei",imei);
                            intent.putExtra("dbname",dbname);
                            intent.putExtra("name",name_person);


                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(SignUpActivity.this,msg_, Toast.LENGTH_LONG).show();
                            Loader.stopLoading();
                        }

                    }

                    break;
                case 1:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        et_station = findViewById(R.id.et_station);
        uhfUtils = UHFUtils.getInstance();
        networkAvailable = uhfUtils.isNetworkAvailable(this);
        userName = getIntent().getStringExtra("userName");//数据库名字
        pwd = getIntent().getStringExtra("pwd");
        imei = getIntent().getStringExtra("imei");
        dbname = getIntent().getStringExtra("dbname");

        name_person = getIntent().getStringExtra("name");
        mmname = getIntent().getStringExtra("mmname");
        uuname = getIntent().getStringExtra("uuname");




    }

    public void click_signUp(View view) {
        staionName = et_station.getText().toString();
        if (TextUtils.isEmpty(staionName)){
            Toast.makeText(this, "请填写站点", Toast.LENGTH_SHORT).show();
        }else {
            Loader.showLoading(this);
            if (networkAvailable)signUp();
        }
    }

    private void signUp() {
       Loader.showLoading(SignUpActivity.this);
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("dbname",dbname);
        builder.add("username", userName);
        builder.add("password", pwd);
        builder.add("MacCode", imei);
        builder.add("macName", staionName);

        OkhttpUtils.post_GetJson(GloableUrl.registe, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(SignUpActivity.this, "请检查网络或联系管理员", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    Log.e("SignUpActivity", "onResponse: "+ json);
                    Map<String,String> map =  ParJson.getSignupMap(json);

                    Message message = Message.obtain();
                    message.what = 0;
                    message.obj = map;
                    handler.sendMessage(message);
            }
        },builder);

    }

    public void click_cancel(View view) {
        Loader.showLoading(this);
          finish();
    }

    @Override
    public void onClick(View v) {

    }
}
